package fr.inserm.umr915.libjson;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.w3c.dom.Document;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

public abstract class BooleanNode extends Node
	{
	static final String TAG="boolean";
	static abstract class TrueNode
		extends BooleanNode
		{
		@Override
		public boolean getValue()
			{
			return true;
			}
		}
	
	static abstract class FalseNode
		extends BooleanNode
		{
		@Override
		public boolean getValue()
			{
			return false;
			}
		}
	
	protected BooleanNode()
		{	
		}
	
	public abstract boolean getValue();
	
	@Override
	public Node clone(NodeFactory owner) {
		return owner.createBooleanNode(getValue());
		}
	
	@Override
	public int hashCode()
		{
		return 1+31*(getValue()?0:1);
		}
	
	@Override
	public NodeType getNodeType()
		{
		return NodeType.BOOLEAN;
		}
	@Override
	public String toString()
		{
		return String.valueOf(getValue());
		}
	@Override
	public void print(PrintWriter out)
		{
		out.print(getValue());
		}
	
	@Override
	public void write(DataOutputStream out) throws IOException
		{
		out.writeByte((byte)getNodeType().ordinal());
		out.writeBoolean(getValue());
		}
	
	@Override
	public org.w3c.dom.Element asDom(Document owner)
		{
		org.w3c.dom.Element root= owner.createElement(TAG);
		root.appendChild(owner.createTextNode(String.valueOf(getValue())));
		return root;
		}
	
	@Override
	public void write(XMLStreamWriter out) throws XMLStreamException
		{
		out.writeStartElement(TAG);
		out.writeCharacters(String.valueOf(getValue()));
		out.writeEndElement();
		}
	
	@Override
	protected void saxHandler(ContentHandler handler) throws SAXException,
			IOException
		{
		handler.startElement("", "", TAG, new AttributesImpl());
		char ch[]=String.valueOf(getValue()).toCharArray();
		handler.characters(ch, 0, ch.length);
		handler.endElement("", "",TAG);
		}
	
}
